import type { PageType, PageTypeInstance } from './type'
import {
  AppStyleCollections,
  BodyAttrs,
  HOSTNAME,
  SUPPORTED_PLATFORMS,
} from '../../types/constants'
import { StorageProcessor } from '../../user_center/tab/personalization/custom_css/theme/others/greasy_fork/module/storage_process'
import { GreasyFork } from '../../user_center/tab/personalization/custom_css/theme/others/greasy_fork/page/lib'
import { IconService } from '../icons'
import { ToastManager } from '../swal_toast'
import { PageBadgeHandler } from './badage'
import { PageButtonHandler } from './button'
import { BaiduPage } from './platform/baidu'
import { BasePage } from './platform/base'
import { BianPage } from './platform/bian'
import { BingPage } from './platform/bing'
import { GiteePage } from './platform/gitee'
import { GithubPage } from './platform/github'
import { GooglePage } from './platform/google'
import { GreasyForkPage } from './platform/greasyfork'
import { IconfontPage } from './platform/iconfont'
import { SoPage } from './platform/so'
import { SogouPage } from './platform/sogou'
import { ZhiHuQuestionPage } from './platform/zhi_hu_question'
import { PageUserHandler } from './user'

// 平台服务接口
interface PlatformServices {
  greasyFork: GreasyFork
}

// 平台页面处理器
interface PlatformPageHandlers {
  base: BasePage
  baidu: BaiduPage
  google: GooglePage
  bing: BingPage
  sogou: SogouPage
  so: SoPage
  greasyFork: GreasyForkPage
  iconfont: IconfontPage
  bian: BianPage
  zhiHuQuestion: ZhiHuQuestionPage
  gitee: GiteePage
  github: GithubPage
}

// 平台页面存储
interface PlatformPageStorages {
  greasyFork: StorageProcessor
}

// 页面功能模块
interface PageFeatures {
  button: PageButtonHandler
  badge: PageBadgeHandler
  user: PageUserHandler
}

// 页面服务模块
interface PageUIServices {
  icon: IconService
  toast: ToastManager
}

export class PageHandler {
  private static instance: PageHandler
  private commentId: string | null = null

  // 平台服务
  readonly services: PlatformServices = {
    greasyFork: GreasyFork.getInstance(),
  }

  // 页面处理器
  readonly pages: PlatformPageHandlers = {
    base: BasePage.getInstance(),
    baidu: BaiduPage.getInstance(),
    google: GooglePage.getInstance(),
    bing: BingPage.getInstance(),
    sogou: SogouPage.getInstance(),
    so: SoPage.getInstance(),
    greasyFork: GreasyForkPage.getInstance(),
    iconfont: IconfontPage.getInstance(),
    bian: BianPage.getInstance(),
    zhiHuQuestion: ZhiHuQuestionPage.getInstance(),
    gitee: GiteePage.getInstance(),
    github: GithubPage.getInstance(),
  }

  // 页面存储
  readonly storages: PlatformPageStorages = {
    greasyFork: StorageProcessor.getInstance(),
  }

  // 功能模块
  readonly features: PageFeatures = {
    button: PageButtonHandler.getInstance(),
    badge: PageBadgeHandler.getInstance(),
    user: PageUserHandler.getInstance(),
  }

  // UI 模块
  readonly ui: PageUIServices = {
    icon: IconService.getInstance(),
    toast: ToastManager.getInstance(),
  }

  /**
   * 获取单例实例
   */
  public static getInstance(): PageHandler {
    if (!PageHandler.instance) {
      PageHandler.instance = new PageHandler()
    }
    return PageHandler.instance
  }

  /**
   * 获取当前页面实例
   */
  public static getCurrentPageInstance(): PageTypeInstance {
    switch (HOSTNAME) {
      case SUPPORTED_PLATFORMS.GREASY_FORK:
        return GreasyForkPage.getInstance()
      case SUPPORTED_PLATFORMS.BAI_DU:
        return BaiduPage.getInstance()
      case SUPPORTED_PLATFORMS.GOOGLE:
        return GooglePage.getInstance()
      case SUPPORTED_PLATFORMS.BING:
        return BingPage.getInstance()
      case SUPPORTED_PLATFORMS.SO_GOU:
        return SogouPage.getInstance()
      case SUPPORTED_PLATFORMS.SO:
        return SoPage.getInstance()
      case SUPPORTED_PLATFORMS.ICONFONT:
        return IconfontPage.getInstance()
      case SUPPORTED_PLATFORMS.BIAN:
        return BianPage.getInstance()
      case SUPPORTED_PLATFORMS.ZHI_HU_QUESTION:
        return ZhiHuQuestionPage.getInstance()
      case SUPPORTED_PLATFORMS.GITEE:
        return GiteePage.getInstance()
      case SUPPORTED_PLATFORMS.GITHUB:
        return GithubPage.getInstance()
      default:
        return undefined
    }
  }

  /**
   * 检测当前页面类型
   */
  public static detectCurrentPageType(): PageType {
    return PageHandler.getCurrentPageInstance()?.pageType || 'default'
  }

  /**
   * 获取当前页面类型
   */
  public static getCurrentPageType(): PageType {
    return (document.body.getAttribute(BodyAttrs.PAGE_TYPE) || 'default') as PageType
  }

  /**
   * 滚动到指定评论
   */
  scrollToComment(commentId: string): void {
    const targetElement = document.getElementById(commentId)

    if (targetElement) {
      if (this.commentId && this.commentId !== commentId) {
        document.getElementById(this.commentId)?.classList.remove(AppStyleCollections.Body.SCROLL_HIGHLIGHT)
      }

      // 记录当前滚动位置
      this.commentId = commentId

      targetElement.scrollIntoView({ behavior: 'smooth', block: 'center' })
      // 添加高亮效果
      targetElement.classList.add(AppStyleCollections.Body.SCROLL_HIGHLIGHT)
      setTimeout(() => {
        targetElement.classList.remove(AppStyleCollections.Body.SCROLL_HIGHLIGHT)
      }, 2000)
    }
  }
}
